package com.example.cc.server.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;

import javax.annotation.PostConstruct;

@Configuration
@RequiredArgsConstructor
@Slf4j
public class RabbitTemplateConfig {

    private final RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        // rabbitTemplate.setExchange("exchange");
        // rabbitTemplate.setRoutingKey("routingKey");
        rabbitTemplate.setEncoding("UTF-8");
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(@NonNull ReturnedMessage returnedMessage) {
                log.info("【publisher-returns消息发送失败】，Exchange:{}, RoutingKey:{}, Message:{}, ReplyCode:{}, ReplyText:{}",
                        returnedMessage.getExchange(),
                        returnedMessage.getRoutingKey(),
                        returnedMessage.getMessage(),
                        returnedMessage.getReplyCode(),
                        returnedMessage.getReplyText()
                );
            }
        });
    }

}
